import {
  createRouter,
  createWebHashHistory,
  NavigationGuardNext,
  RouteLocationNormalized
} from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import store from '@/store'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      keepAlive: false,
      title: '登录页面',
      closable: true
    }
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/main.vue'),
    meta: {
      keepAlive: false,
      title: '主页面',
      closable: true
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/not-found/not-found.vue'),
    meta: {
      keepAlive: false,
      title: '未知的页面',
      closable: true
    }
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory()
})

router.beforeEach(
  (
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ) => {
    if (to.meta.keepAlive) store.commit('tag/changeCachedList', to)

    next()
  }
)

export default router
